package com.lanju.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.lanju.dao.HouseTransferListDao;
import com.lanju.entity.HouseTransferList;
import com.lanju.entity.HouseTransferListItem;
import com.lanju.model.ResultModel;
import com.lanju.service.HouseTransferListService;


@Service
public class HouseTransferListServiceImpl implements HouseTransferListService{

    private static final Logger LOGGER = LoggerFactory.getLogger(HouseTransferListServiceImpl.class);
    
    @Autowired
    HouseTransferListDao entityDao;

    /** 新增 */
    @Override
    @Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
    public ResultModel add(HouseTransferList entity) {
        try {
        	if(!StringUtils.isEmpty(entity.getTransferId())){
        		synchronized (entity) {
        			entityDao.removeItem(entity.getTransferId());
				}
        		entityDao.merge2(entity);
        	}else{
        		entityDao.persist(entity);
        	}
        	
            return ResultModel.success("录入成功", null);
        } catch (Exception e) {
            LOGGER.error("addHouseCheckList fail",e);
        }
        return ResultModel.fail("录入失败", null);
    }

    /** 根据id删除 */
    @Override
    @Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
    public ResultModel del(HouseTransferList entity) {
    	String entityId = entity.getHouseId();
        HouseTransferList obj = entityDao.getById(entityId);
        if(obj==null){
            return ResultModel.fail("HouseCheckList不存在", null);
        }
        try {
            entityDao.remove(entity);;
            return ResultModel.success("删除成功", null);
        } catch (Exception e) {
            LOGGER.error("delHouseCheckList fail",e);
        }
        return ResultModel.fail("删除失败", null);
    }
    
    /** 根据id修改 */
    @Override
    @Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
    public ResultModel edit(HouseTransferList entity) {
        HouseTransferList obj = entityDao.getById(entity.getHouseId());
        if(obj==null){
            return ResultModel.fail("HouseCheckList不存在", null);
        }
        try {
            entityDao.merge(entity);
            return ResultModel.success("编辑成功", null);
        } catch (Exception e) {
            LOGGER.error("editHouseCheckList fail",e);
        }
        return ResultModel.fail("编辑失败", null);
    }

    /** 根据id获取 */
    @Override
    @Transactional(value="financialmarketingTM", readOnly=true)
    public HouseTransferList getById(String id) {
        HouseTransferList obj = entityDao.getById(id);
        return obj;
    }
    
    
    
    /** 根据参数获取 */
    @Override
    @Transactional(value="financialmarketingTM", readOnly=true)
    public HouseTransferList getByParam(HouseTransferList entity) {
        HouseTransferList obj = entityDao.getByParam(entity);
        return obj;
    }
    
    

    /** 分页查询 */
    @Override
    @Transactional(value="financialmarketingTM", readOnly=true)
    public Map<String, Object> getList(Map param, int pageSize, int offset) {
    	 HashMap<String, Object> rsMap = new HashMap<String, Object>();
    	 long count = entityDao.getListCount(param);
         List<HouseTransferList> list = entityDao.getList(param, pageSize, offset);
         rsMap.put("total", count);
         rsMap.put("rows", list);
        return rsMap;
    }
    
    
    @Override
    @Transactional(value="financialmarketingTM", readOnly=true)
    public List<Map> getBaseItemList(Map param) {
        return entityDao.getBaseItemList(param);
    }
    
    @Override
    @Transactional(value="financialmarketingTM", readOnly=true)
    public List<Map> getHouseList(Map param) {
        return entityDao.getHouseList(param);
    }

	@Override
	public List<HouseTransferListItem> getHouseTransferListItem(String id) {
		return entityDao.getItemById(id);
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)	
	public Map<String, Object> delTransfer(HouseTransferList entity) {
		Map<String,Object> result = new HashMap<>();
		try {
			synchronized (entity) {
				entityDao.removeItem(entity.getTransferId());
			}
			entityDao.delTransfer(entity);
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "删除失败");
		}
		return result;
	}
  
}
